Complicated list comprehension with many loop in Python